package com.abe.libcore.base.screen

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.abe.libquick.utils.NormalUtils
import java.util.*

abstract class AbeFragment : BScreenFragment() {
    private val appToast: Int = 0x999
    lateinit var rootView: View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        rootView = inflater.inflate(getLayoutRes(), container, false)
        if (isAutoZoom() && getScale() != 1.0F)
            RelayoutTool.relayoutViewHierarchy(rootView, getScale())
        initContent()
        return rootView
    }

    protected abstract fun getLayoutRes(): Int

    protected abstract fun initContent()

    /**********************************************************************************************/
    open fun getEmpty(value: String?): String? {
        return if (TextUtils.isEmpty(value)) "" else value
    }

    open fun getText(editText: EditText?): String? {
        return editText?.text?.toString()?.trim { it <= ' ' } ?: ""
    }

    open fun getText(textView: TextView?): String? {
        return textView?.text?.toString()?.trim { it <= ' ' } ?: ""
    }

    open fun stringToDate(dateStr: String?, rule: String?): Date? {
        return NormalUtils.stringToDate(rule, dateStr)
    }

    open fun dateToString(date: Date?, rule: String?): String? {
        return NormalUtils.dateToString(rule, date)
    }

    /**********************************************************************************************/
    inner class BaseHandler(looper: Looper) : Handler(looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                appToast -> Toast.makeText(context, msg.obj.toString(), Toast.LENGTH_SHORT).show()
                else -> if (isImp(this@AbeFragment.javaClass, OnHandlerListener::class.java)) {
                    (this@AbeFragment as OnHandlerListener).dispatchHandleMessage(msg)
                }
            }
        }
    }

    protected open fun toast(content: String?) {
        if (TextUtils.isEmpty(content)) return
        localSendMsg(appToast, content)
    }

    open fun toast(strRes: Int) {
        toast(resources.getString(strRes))
    }

    open fun toast(rule: String?, vararg objects: Any?) {
        toast(String.format(Locale.CHINA, rule!!, *objects))
    }

    protected open fun localSendMsg(what: Int, obj: Any?) {
        getHandler().sendMessage(getMessage(what, obj!!))
    }

    open fun localEmptyMsg(what: Int) {
        getHandler().sendEmptyMessage(what)
    }

    //获取Message
    private fun getMessage(what: Int, obj: Any): Message {
        val msg = Message()
        msg.what = what
        msg.obj = obj
        return msg
    }

    //获取Handler
    protected fun getHandler(): BaseHandler {
        return BaseHandler(Looper.getMainLooper())
    }

    /**
     * 是否实现接口
     * @param a 类
     * @param b 接口
     */
    protected fun isImp(a: Class<*>, b: Class<*>): Boolean {
        return b.isAssignableFrom(a)
    }
}